<template>
	<view class="dynamic-technician-list" :style="{background:pageColor}">
		<block v-if="isLoad">
			<fixed>
				<view :style="{background:pageColor}">
					<view class="count-list flex-center fill-base">
						<view @tap.stop="$util.goUrl({url:`/dynamic/pages/technician/thumbs`})"
							class="count-item flex-center flex-column f-caption c-title">
							<view class="tag thumbs flex-center rel">
								<i class="iconfont icon-shoucang-fill"></i>
								<view class="count-tag flex-center f-icontext c-base abs"
									:style="{width: count.thumbs_num < 10 ? '26rpx':'50rpx',right:count.thumbs_num < 10 ? '-13rpx':'-38rpx'}"
									v-if="count.thumbs_num">
									{{count.thumbs_num < 100 ? count.thumbs_num : '99+'}}
								</view>
							</view>
							<view class="mt-md">收获的赞</view>
						</view>
						<view @tap.stop="$util.goUrl({url:`/dynamic/pages/technician/follow`})"
							class="count-item flex-center flex-column f-caption c-title">
							<view class="tag follow flex-center rel">
								<i class="iconfont iconxinzengguanzhu"></i>
								<view class="count-tag flex-center f-icontext c-base abs"
									:style="{width: count.follow_num < 10 ? '26rpx':'50rpx',right:count.follow_num < 10 ? '-13rpx':'-38rpx'}"
									v-if="count.follow_num">
									{{count.follow_num < 100 ? count.follow_num : '99+'}}
								</view>
							</view>
							<view class="mt-md">新增关注</view>
						</view>
						<view @tap.stop="$util.goUrl({url:`/dynamic/pages/technician/comment`})"
							class="count-item flex-center flex-column f-caption c-title">
							<view class="tag comment flex-center rel">
								<i class="iconfont iconshouhuodepinglun"></i>
								<view class="count-tag flex-center f-icontext c-base abs"
									:style="{width: count.comment_num < 10 ? '26rpx':'50rpx',right:count.comment_num < 10 ? '-13rpx':'-38rpx'}"
									v-if="count.comment_num">
									{{count.comment_num < 100 ? count.comment_num : '99+'}}
								</view>
							</view>
							<view class="mt-md">收获的评论</view>
						</view>
					</view>
					<view class="rel">
						<view class="flex-between pd-lg">
							<view class="f-title text-bold">我的发布</view>
							<view @tap.stop="toChangeItem(-1)" class="flex-y-center f-desc">
								{{statusList[statusInd].title}}<i class="iconfont c-desc ml-sm"
									:class="[{'iconshaixuanxia-1':showSort},{'iconshaixuanshang-1':!showSort}]"
									style="font-size: 20rpx"></i>
							</view>
						</view>
						<view class="dynamic-sort pt-md pb-md f-desc abs" v-if="showSort">
							<view @tap.stop="toChangeItem(index)" class="sort-item flex-center"
								:style="{color:statusInd == index ? primaryColor : ''}"
								v-for="(item,index) in statusList" :key="index">
								{{item.title}}
							</view>
						</view>
					</view>
				</view>
			</fixed>

			<wfalls-flow :list="list.data" :path="2" ref="wfalls" v-if="list.data.length > 0"></wfalls-flow>
			<load-more :noMore="list.current_page>=list.last_page&&list.data.length>0" :loading="loading"
				v-if="loading">
			</load-more>
			<block v-if="!loading&&list.data.length<=0&&list.current_page==1">
				<abnor v-if="location.lat&&location.lng" percent="calc(100vh - 312rpx - calc(30rpx + env(safe-area-inset-bottom) / 2))">
				</abnor>
				<block v-if="!location.lat&&!location.lng">
					<!-- #ifdef H5 -->
					<abnor type="NOT_LOCATION" title="暂无数据" percent="calc(100vh - 312rpx - calc(30rpx + env(safe-area-inset-bottom) / 2))"></abnor>
					<!-- #endif -->
					<!-- #ifndef H5 -->
					<abnor type="NOT_LOCATION" title="暂无数据" @confirm="toOpenLocation" percent="calc(100vh - 312rpx - calc(30rpx + env(safe-area-inset-bottom) / 2))"
						:button="[{ text: '开启定位' , type: 'confirm' }]" btnSize=""></abnor>
					<!-- #endif -->
				</block>
			</block>
			<view class="space-footer"></view>
		</block>
		<!-- #ifdef MP-WEIXIN -->
		<user-privacy ref="user_privacy" :show="false"></user-privacy>
		<!-- #endif -->
		<!-- #ifdef APP-PLUS -->
		<longbingbing-app-check-auth type="location" ref="app_check_item"
			@confirm="toConfirmOpenLoca"></longbingbing-app-check-auth>
		<!-- #endif -->
	</view>
</template>

<script>
	import {
		mapState,
		mapActions,
		mapMutations
	} from "vuex"
	import wfallsFlow from "@/components/wfalls-flow.vue"
	export default {
		components: {
			wfallsFlow
		},
		data() {
			return {
				isLoad: false,
				statusList: [{
					id: 0,
					title: '全部'
				}, {
					id: 1,
					title: '审核中'
				}, {
					id: 2,
					title: '审核通过'
				}, {
					id: 3,
					title: '已驳回'
				}],
				statusInd: 0,
				showSort: false,
				loading: true,
				param: {
					page: 1,
					limit: 10,
					status: 0
				},
				list: {
					data: []
				},
				count: {}
			}
		},
		computed: mapState({
			configInfo: state => state.config.configInfo,
			haveShieldOper: state => state.user.haveShieldOper,
		}),
		onLoad() {
			this.$util.setNavigationBarColor({
				bg: this.primaryColor
			})
			this.initIndex()
		},
		async onShow() {
			if (this.haveShieldOper == 1) {
				await this.initRefresh()
				this.updateUserItem({
					key: 'haveShieldOper',
					val: 0
				})
			} else {
				this.getDynamicData()
			}
			if (!this.location.lat && this.locaRefuse) {
				this.toResetUtilLoca()
			}
		},
		async onPullDownRefresh() {
			// #ifndef APP-PLUS
			uni.showNavigationBarLoading()
			// #endif
			await this.initRefresh();
			uni.stopPullDownRefresh()
		},
		async onReachBottom() {
			if (this.list.current_page >= this.list.last_page || this.loading) return;
			this.param.page = this.param.page + 1;
			this.loading = true;
			await this.getList();
			setTimeout(() => {
				this.$refs.wfalls.handleViewRender();
			}, 0)
		},
		watch: {
			locaRefuse(newval, oldval) {
				if (!newval) {
					this.toResetUtilLoca()
				}
			}
			// #ifndef H5
			,
			changeOnAddr(newval, oldval) {
				let {
					noloca,
				} = this.noChangeLoca
				if (newval && noloca) {
					this.initUtilLocaData()
				}
			},
			noChangeLoca(newval, oldval) {
				setTimeout(() => {
					let {
						lat,
						lng,
						unix = 0
					} = this.changeOnAddr
					let {
						noloca
					} = this.noChangeLoca
					let cur_unix = this.$util.DateToUnix(this.$util.formatTime(new Date(), 'YY-M-D h:m:s'))
					if (noloca && ((!lat && !lng) || !unix || (unix && (cur_unix - unix >= 1)))) {
						this.getUtilLocation()
					}
				}, 800)
			}
			// #endif
		},
		methods: {
			...mapMutations(['updateUserItem', 'updateMapItem']),
			async initIndex(refresh = false) {
				await Promise.all(refresh ? [this.getDynamicData(), this.isNotLoca()] : [this.isNotLoca()])

				// #ifdef H5
				if (!refresh) {
					this.$jweixin.hideOptionMenu()
				}
				// #endif
			},
			async initRefresh() {
				await this.initIndex(true)
			},
			async toResetUtilLoca() {
				// #ifdef APP-PLUS
				let {
					lat: locaLat = 0
				} = this.location
				let {
					appCheckLocaStatus = null
				} = this
				if (!locaLat && appCheckLocaStatus == 1) {
					this.isLoad = false
					let {
						lng = 0,
							lat = 0
					} = await this.$util.getUtilLocation()
					this.isLoad = true
					if (!lat && !lng) return
					await this.initRefresh()
				}
				// #endif
			},
			async toOpenLocation() {
				// #ifdef APP-PLUS
				if (plus.os.name == 'Android' && plus.navigator.checkPermission(
						'android.permission.ACCESS_FINE_LOCATION') ===
					'undetermined') {
					this.$refs.app_check_item.open()
				} else {
					this.toConfirmOpenLoca()
				}
				// #endif
				// #ifndef APP-PLUS
				this.toConfirmOpenLoca()
				// #endif
			},
			async toConfirmOpenLoca() {
				// #ifdef MP-WEIXIN
				let privacyCheck = this.$refs.user_privacy.check()
				if (privacyCheck) {
					this.$refs.user_privacy.open()
					return
				}
				// #endif
				this.initRefresh()
			},
			async isNotLoca() {
				let {
					location,
					locaRefuse
				} = this
				let {
					status: coach_status = 0,
					coach_position = 0
				} = this.userCoachStatus



				// #ifdef H5
				if (coach_position && coach_status == 2 && (location.lat && location.address != '暂未获取到位置信息')) {
					this.updateUserItem({
						key: 'isChangeCoachLoca',
						val: true
					})
				}
				// #endif


				// #ifdef APP-PLUS
				if (!locaRefuse && (!location.lat || (location.lat && location.address == '暂未获取到位置信息'))) {
					// #endif
					// #ifndef APP-PLUS
					if (!location.lat || (location.lat && location.address == '暂未获取到位置信息')) {
						// #endif

						// #ifdef MP-WEIXIN
						let privacyCheck = this.$refs.user_privacy.check()
						if (privacyCheck || typeof(privacyCheck) == 'string') {
							if (privacyCheck) {
								this.$refs.user_privacy.open()
							}
							this.isLoad = true
							this.loading = false
							this.$util.hideAll()
							return
						}
						// #endif

						// #ifndef H5
						if (coach_position && coach_status == 2) {
							let {
								lat: change_lat = 0,
								lng: change_lng = 0,
								unix = 0
							} = this.changeOnAddr
							let cur_unix = this.$util.DateToUnix(this.$util.formatTime(new Date(), 'YY-M-D h:m:s'))
							let noloca = change_lat && change_lng && (unix && (cur_unix - unix < 3)) ? false : true
							if (!noloca) {
								let loca = Object.assign({}, this.location, {
									lat: change_lat,
									lng: change_lng,
									is_util_loca: 1
								})
								this.updateUserItem({
									key: 'location',
									val: loca
								})
							}
							this.updateUserItem({
								key: 'noChangeLoca',
								val: {
									noloca
								}
							})
							if (this.noChangeLoca.noloca) return
							this.initUtilLocaData()
						} else {
							this.getUtilLocation()
						}
						this.updateMapItem({
							key: 'pageActive',
							val: false
						})
						return
						// #endif

						// #ifdef H5
						await this.$util.getUtilLocation()
						this.updateMapItem({
							key: 'pageActive',
							val: false
						})
						// #endif
					}

					this.initUtilLocaData()
				},
				async getUtilLocation() {
						await this.$util.getUtilLocation()
						this.initUtilLocaData()
					},
					async initUtilLocaData() {
							// #ifndef H5
							this.updateUserItem({
								key: 'noChangeLoca',
								val: {
									noloca: false
								}
							})
							// #endif 
							await this.getList(1)
							let {
								lat = 0, lng = 0, is_util_loca = 0, isIp = true
							} = this.location
							if (lat && lng && is_util_loca) {
								this.$util.getMapInfo(isIp)
							}
						},
						async getDynamicData() {
								this.count = await this.$api.dynamic.dynamicData()
							},
							async getList(page) {
									if (page) {
										this.showSort = false
										this.param.page = 1
										this.list.data = []
										uni.pageScrollTo({
											scrollTop: 0
										})
									}
									let {
										lat = 0, lng = 0
									} = this.location

									let {
										list: oldList,
										statusList,
										statusInd
									} = this
									let {
										id: status
									} = statusList[statusInd]
									let param = Object.assign({}, this.param, {
										lat,
										lng,
										status
									})

									let newList = await this.$api.dynamic.coachDynamicList(param)
									if (this.param.page == 1) {
										this.list = newList
									} else {
										newList.data = oldList.data.concat(newList.data)
										this.list = newList
									}
									this.isLoad = true
									this.loading = false
									this.$util.hideAll()
								},
								toChangeItem(index) {
									if (index == -1) {
										this.showSort = !this.showSort
									} else {
										this.statusInd = index
										this.showSort = false
										this.initRefresh()
									}
								},
			}
		}
</script>

<style lang="scss">
	.dynamic-technician-list {
		.count-list {
			width: 750rpx;
			height: 204rpx;

			.count-item {
				width: 33.33%;

				.tag {
					width: 85rpx;
					height: 85rpx;
					border-radius: 29rpx;

					.iconfont {
						font-size: 44rpx;
					}

					.count-tag {
						top: 0;
						right: -13rpx;
						width: 26rpx;
						height: 26rpx;
						background: #E82F21;
						border-radius: 26rpx;
					}
				}

				.thumbs {
					color: #ff6262;
					background: #FFEFEF;
				}

				.follow {
					color: #FC8218;
					background: #FEF6E7;
				}

				.comment {
					color: #44A860;
					background: #ECF6EF;
				}
			}
		}

		.dynamic-sort {
			top: 80rpx;
			right: 32rpx;
			width: 145rpx;
			background: #FEFFFE;
			box-shadow: 0 14rpx 20rpx 0 rgba(132, 132, 132, 0.08);
			border-radius: 4rpx;
			border: 1rpx solid #EFEFEF;
			transform: rotateZ(360deg);
			z-index: 1;

			.sort-item {
				padding: 15rpx 0;
			}
		}
	}
</style>